package com.egoo.ticket.server.service.impl;

import com.egoo.ticket.server.dao.*;
import com.egoo.ticket.server.pojo.dto.FilterThirdInputDto;
import com.egoo.ticket.server.pojo.dto.FilterThirdOutDto;
import com.egoo.ticket.server.pojo.vo.InteractiveDataVo;
import com.egoo.ticket.server.pojo.vo.WorkOrderTypeVo;
import com.egoo.ticket.server.service.CustomFieldService;
import com.egoo.ticket.server.service.DepartmentService;
import com.egoo.ticket.server.service.FilterThirdService;
import com.egoo.ticket.server.utils.common.ConstantCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 */
@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class FilterThirdServiceImpl implements FilterThirdService {

	@Autowired
	private WorkOrderStatusDao workOrderStatusDao;
	@Autowired
	private EwsUserDao ewsUserDao;
	@Autowired
	private EwsRoleDao ewsRoleDao;
	@Autowired
	private CustomFieldService customFieldService;
	@Autowired
	private WorkOrderTypeNodeDao workOrderTypeNodeDao;
	@Autowired
	private EwsDepartmentDao ewsDepartmentDao;
	@Autowired
	private DepartmentService departmentService;
	@Autowired
	private WorkOrderTypeDao workOrderTypeDao;

	/**
	 * TODO 获取筛选条件的第三部分
	 * 
	 * @param filterThirdInputDto
	 * @return
	 */
	@Override
	public FilterThirdOutDto selectByDto(FilterThirdInputDto filterThirdInputDto) {
		FilterThirdOutDto filterThirdOutDto = new FilterThirdOutDto();
		Integer firstId = filterThirdInputDto.getFirstId();
		String tenantId = filterThirdInputDto.getTenantId();
		// 1为工单标题 2为工单描述 3为客户名称 4为客户电话
		if (ConstantCode.FilterFirst.WORK_ORDER_TITLE_ID == firstId || ConstantCode.FilterFirst.WORK_ORDER_DESCRIBE_ID == firstId
				|| ConstantCode.FilterFirst.CUSTOM_NAME_ID == firstId || ConstantCode.FilterFirst.CUSTOM_PHONE_ID == firstId) {
			// 返回标识1 代表这是个文本框
			filterThirdOutDto.setShapeTag(1);
			// 文本框可输入单个也可能输入多个
			filterThirdOutDto.setTagIn(0);
			// 5为工单状态
		} else if (ConstantCode.FilterFirst.WORK_ORDER_STATUS_ID == firstId) {
			// 返回标识2 是下拉框
			filterThirdOutDto.setShapeTag(2);
			// 1为是 2为不是
			if (ConstantCode.FilterSecond.CONTAIN_ID_ONE == filterThirdInputDto.getSecondDataId() || ConstantCode.FilterSecond.YES_ID_TWO == filterThirdInputDto.getSecondDataId()) {
				// 返回标识1代表下拉框只能选中1个
				filterThirdOutDto.setTagIn(1);
			} else {
				// 返回标识3代表下拉框可选中多个
				filterThirdOutDto.setTagIn(3);
			}
			// 如果输入的name不为空，就去调用工单状态的模糊查询接口
			if (null != filterThirdInputDto.getName()) {
				List workOrderStatusList = workOrderStatusDao.selectAllStatusByNames(filterThirdInputDto.getName());
				// 将模糊查询出来工单状态list放入输出dto
				filterThirdOutDto.setDataList(workOrderStatusList);
			} else {
				List workOrderStatusList = workOrderStatusDao.selectAllStatuss(tenantId);
				// 查询全部的工单状态list放入输出dto
				filterThirdOutDto.setDataList(workOrderStatusList);
			}
			// firstId为6代表工单优先级
		} else if (ConstantCode.FilterFirst.WORK_ORDER_PRIORITY_ID == firstId) {
			// 返回shapeTag为2代表下拉框
			filterThirdOutDto.setShapeTag(2);
			// 1为是；2为不是
			if (ConstantCode.FilterSecond.CONTAIN_ID_ONE == filterThirdInputDto.getSecondDataId() || ConstantCode.FilterSecond.YES_ID_TWO == filterThirdInputDto.getSecondDataId()) {
				// 返回tagIn为1代表只能选中1个填入
				filterThirdOutDto.setTagIn(1);
			} else {
				// 返回tagIn为3代表可选中多个填入
				filterThirdOutDto.setTagIn(3);
			}
			if (null != filterThirdInputDto.getName()) {
				List<InteractiveDataVo> interactiveDataList = customFieldService
						.selectWorkOrderPriorityByName(filterThirdInputDto.getName(),tenantId);
				filterThirdOutDto.setDataList(interactiveDataList);
			} else {
				List interactiveDataList =
						customFieldService.selectAllSelectFieldListById(ConstantCode.CustomFieldConstant.PRIORITY_ID);
				filterThirdOutDto.setDataList(interactiveDataList);
			}
			// 工单来源
		} else if (ConstantCode.FilterFirst.WORK_ORDER_SOURCE_ID == firstId) {
			// 返回shapeTag为2代表下拉框
			filterThirdOutDto.setShapeTag(2);
			// 1为是；2为不是
			if (ConstantCode.FilterSecond.CONTAIN_ID_ONE == filterThirdInputDto.getSecondDataId() || ConstantCode.FilterSecond.YES_ID_TWO == filterThirdInputDto.getSecondDataId()) {
				// 返回tagIn为1代表只能选中1个填入
				filterThirdOutDto.setTagIn(1);
			} else {
				// 返回tagIn为3代表可选中多个填入
				filterThirdOutDto.setTagIn(3);
			}
			if (null != filterThirdInputDto.getName()) {
				List<InteractiveDataVo> interactiveDataList = customFieldService
						.selectWorkOrderSourceByName(filterThirdInputDto.getName(),tenantId);
				filterThirdOutDto.setDataList(interactiveDataList);
			} else {
				List<InteractiveDataVo> interactiveDataList = customFieldService
						.selectAllSelectFieldListById(ConstantCode.CustomFieldConstant.SOURCE_ID);
				filterThirdOutDto.setDataList(interactiveDataList);
			}

			// 工单节点
		} else if (ConstantCode.FilterFirst.WORK_ORDER_NODE_ID == firstId) {
			// 返回shapeTag为2代表下拉框
			filterThirdOutDto.setShapeTag(2);
			// 1为是；2为不是
			if (ConstantCode.FilterSecond.CONTAIN_ID_ONE == filterThirdInputDto.getSecondDataId() || ConstantCode.FilterSecond.YES_ID_TWO == filterThirdInputDto.getSecondDataId()) {
				// 返回tagIn为1代表只能选中1个填入
				filterThirdOutDto.setTagIn(1);
			} else {
				// 返回tagIn为3代表可选中多个填入
				filterThirdOutDto.setTagIn(3);
			}
			Map<String, Object> searchMap = new HashMap<>(8);
			searchMap.put("tenantId",tenantId);
			searchMap.put("workOrderTypeId",filterThirdInputDto.getWorkOrderTypeId());
			if (StringUtils.isEmpty(filterThirdInputDto.getName())) {
				searchMap.put("nodeName",filterThirdInputDto.getName());
			}
			WorkOrderTypeVo workOrderTypeVo = workOrderTypeDao.selectById(filterThirdInputDto.getWorkOrderTypeId());
			searchMap.put("workOrderTypeVersion",workOrderTypeVo.getMaxVersion());
			List<InteractiveDataVo> interactiveDataList = workOrderTypeNodeDao.selectInteractiveListByCondition(searchMap);
			filterThirdOutDto.setDataList(interactiveDataList);

		} else if (ConstantCode.FilterFirst.WORK_ORDER_CREATOR_ID == firstId || ConstantCode.FilterFirst.WORK_ORDER_CURRENT_RECEIVER_ID == firstId || ConstantCode.FilterFirst.WORK_ORDER_HISTORY_RECEIVER_ID == firstId) {
			// 返回shapeTag为2代表下拉框
			filterThirdOutDto.setShapeTag(2);
			// secondTypeId为user 代表用户
			if (ConstantCode.UserRoleDeptType.USER .equals(filterThirdInputDto.getSecondTypeId())) {
				// SecondDataId 1为是；2为不是
				if (ConstantCode.FilterSecond.CONTAIN_ID_ONE == filterThirdInputDto.getSecondDataId() || ConstantCode.FilterSecond.YES_ID_TWO == filterThirdInputDto.getSecondDataId()) {
					filterThirdOutDto.setTagIn(1);
				} else {
					// 返回tagIn为3代表可选中多个填入
					filterThirdOutDto.setTagIn(3);
				}
				// 对输入名称进行判空操作，如果不为空，就进行用户接口的模糊查询
				if (!StringUtils.isEmpty(filterThirdInputDto.getName()) ) {
					Map<String, Object> stringObjectHashMap = new HashMap<>(2);
					stringObjectHashMap.put("name",
							filterThirdInputDto.getName());
					List<InteractiveDataVo> ewsUserList = ewsUserDao.selectInteractiveUsers(stringObjectHashMap);
					filterThirdOutDto.setDataList(ewsUserList);
					// 如果为空就调用用户接口查询所有用户
				} else {
					Map<String, Object> map = new HashMap<>(4);
					map.put("tenantId",tenantId);
					List<InteractiveDataVo> ewsUserList =
							ewsUserDao.selectInteractiveUsers(map);
					filterThirdOutDto.setDataList(ewsUserList);
				}
				// secondTypeId为role 代表角色
			} else if (ConstantCode.UserRoleDeptType.ROLE .equals(filterThirdInputDto.getSecondTypeId())) {
				if (ConstantCode.FilterSecond.CONTAIN_ID_ONE == filterThirdInputDto.getSecondDataId() || ConstantCode.FilterSecond.YES_ID_TWO == filterThirdInputDto.getSecondDataId()) {
					filterThirdOutDto.setTagIn(1);
				} else {
					// SecondDataId 3为包含任意；4为不包含任意
					filterThirdOutDto.setTagIn(3);
				}
				// 对输入名称进行判空操作，如果不为空，就进行角色接口的模糊查询
				if (null != filterThirdInputDto.getName()) {
					HashMap<String, Object> searchmap = new HashMap<>(2);
					searchmap.put("name",filterThirdInputDto.getName());
					searchmap.put("tenantId",tenantId);
					List<InteractiveDataVo> ewsRoleList =
							ewsRoleDao.selectRolesInteractiveByCondition(searchmap);
					filterThirdOutDto.setDataList(ewsRoleList);
				} else {
					HashMap<String, Object> hashMap = new HashMap<>(4);
					hashMap.put("tenantId",tenantId);
					// 如果为空就调用角色接口查询所有角色
					List<InteractiveDataVo> ewsRoleList =
							ewsRoleDao.selectRolesInteractiveByCondition(hashMap);
					filterThirdOutDto.setDataList(ewsRoleList);
				}

			}else {
				//secondTypeId为dept 代表部门
				if (ConstantCode.FilterSecond.CONTAIN_ID_ONE == filterThirdInputDto.getSecondDataId() || ConstantCode.FilterSecond.YES_ID_TWO == filterThirdInputDto.getSecondDataId()) {
					filterThirdOutDto.setTagIn(1);
				} else {
					// SecondDataId 3为包含任意；4为不包含任意
					filterThirdOutDto.setTagIn(3);
				}
				//对对输入名称进行判空操作，如果不为空，就进行部门接口的模糊查询

				if (StringUtils.isNotBlank( filterThirdInputDto.getName())) {
					HashMap<String, Object> searchmap = new HashMap<>(2);
					searchmap.put("name",filterThirdInputDto.getName());
					List<InteractiveDataVo> ewsDeptList =
							ewsDepartmentDao.selectInteractiveListByCondition(searchmap);
					filterThirdOutDto.setDataList(ewsDeptList);
				} else {
					// 如果为空就调用角色接口查询所有角色
					List<InteractiveDataVo> ewsDeptList =
							departmentService.getDepartmentTreeData(tenantId);
					filterThirdOutDto.setDataList(ewsDeptList);
				}
			}
			// 12为工单发起时间；13为工单完成时间
		} else if (ConstantCode.FilterFirst.WORK_ORDER_INITIATE_TIME_ID == firstId || ConstantCode.FilterFirst.WORK_ORDER_COMPLATE_TIME_ID == firstId) {
			// shapeTag:3代表时间日历
			filterThirdOutDto.setShapeTag(3);
			// secondDataId为1代表早于；secondDataId为2代表晚于；
			if (ConstantCode.FilterSecond.CONTAIN_ID_ONE == filterThirdInputDto.getSecondDataId() || ConstantCode.FilterSecond.YES_ID_TWO == filterThirdInputDto.getSecondDataId()) {
				// 返回tagIn为1代表只可选填入一个
				filterThirdOutDto.setTagIn(1);
			} else {
				// 返回tagIn为2代表只可选填入2个
				filterThirdOutDto.setTagIn(2);
			}
			// firstId：14 为工单催单次数
		} else if (ConstantCode.FilterFirst.WORK_ORDER_URGE_COUNT_ID == firstId) {
			// 4为数字文本框
			filterThirdOutDto.setShapeTag(4);
			filterThirdOutDto.setTagIn(1);
		} else if(ConstantCode.FilterFirst.WORK_ORDER_CREATED_TIME_ID == firstId || ConstantCode.FilterFirst.WORK_ORDER_CLOSED_TIME_ID == firstId){
			// 15为工单创建后时长；16为工单关闭后时长
			// shapeTag为5 代表文本框（数字）后面加小时
			filterThirdOutDto.setShapeTag(5);
			// secondDataId为1,代表大于；2 代表小于
			if (ConstantCode.FilterSecond.CONTAIN_ID_ONE == filterThirdInputDto.getSecondDataId() || ConstantCode.FilterSecond.YES_ID_TWO == filterThirdInputDto.getSecondDataId()) {
				// 返回tagIn为1代表文本框只可填入1个
				filterThirdOutDto.setTagIn(1);
			} else {
				// 返回tagIn为2代表文本框只可填入2个
				filterThirdOutDto.setTagIn(2);
			}
		} else if (ConstantCode.FilterFirst.URGE_STATUS_ID == firstId
				|| ConstantCode.FilterFirst.CALL_STATUS_ID == firstId) {
			// 返回标识2 是下拉框
			filterThirdOutDto.setShapeTag(2);
			// 1为是 2为不是
			if (ConstantCode.FilterSecond.CONTAIN_ID_ONE == filterThirdInputDto.getSecondDataId()
					|| ConstantCode.FilterSecond.YES_ID_TWO == filterThirdInputDto.getSecondDataId()) {
				// 返回标识1代表下拉框只能选中1个
				filterThirdOutDto.setTagIn(1);
			} else {
				// 返回标识3代表下拉框可选中多个
				filterThirdOutDto.setTagIn(3);
			}
		} else if (ConstantCode.FilterFirst.MONITOR_ID == firstId){
			// 返回标识6 无需选择
			filterThirdOutDto.setShapeTag(6);
			// 无需输入
			filterThirdOutDto.setTagIn(6);
		}else if (ConstantCode.FilterFirst.TIME_OUT == firstId){
			filterThirdOutDto.setShapeTag(2);
			filterThirdOutDto.setTagIn(1);
			InteractiveDataVo timeOut = new InteractiveDataVo("Y","超时",null,0,null,null);
			InteractiveDataVo nonTimeOut = new InteractiveDataVo("N","未超时",null,0,null,null);
			ArrayList<InteractiveDataVo> interactiveDataVos = new ArrayList<>();
			interactiveDataVos.add(timeOut);
			interactiveDataVos.add(nonTimeOut);
			filterThirdOutDto.setDataList(interactiveDataVos);
		}
		return filterThirdOutDto;
	}

}